package cn.qyd.threadlocal;

import cn.qyd.common.Person;

/**
 * @author 邱运铎
 * @date 2024-03-22 23:05
 */
public class ThreadLocalDemo {
    private static ThreadLocal<Person> threadLocal = new ThreadLocal<Person>() {
        @Override
        protected Person initialValue() {
            return new Person("default",18);
        }
    };
    private static ThreadLocal<String> threadLocalStr = ThreadLocal.withInitial(() -> "initValue");
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            System.out.println("Hello world!!!");
            System.out.println(threadLocalStr.get());
            threadLocalStr.set(Thread.currentThread().getName());
            System.out.println(threadLocalStr.get());
            threadLocalStr.remove();
            System.out.println(threadLocalStr.get());
            System.out.println("Hello World!!!");
        });
        thread.start();
        System.out.println(threadLocal.get());
        System.out.println(threadLocalStr.get());
        threadLocalStr.set(Thread.currentThread().getName());
        System.out.println(threadLocalStr.get());


    }
}
